﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Dao
{
    public class CS_JCCYYSet_Dao
    {  /// <summary>
        /// 新增一条记录
        /// </summary>
        /// <param name="entity">记录实体</param>
        /// <returns>操作影响的行数</returns>
        public int Add(CS_JCCYY entity)
        {
            using (var context = new Dao.ErpEntities())
            {
                context.CS_JCCYYSet.AddObject(entity);
                return context.SaveChanges();
            }
        }

        /// <summary>
        /// 更新一条记录
        /// </summary>
        /// <param name="entity">记录实体</param>
        /// <returns>操作影响的行数</returns>
        public int Update(CS_JCCYY entity)
        {
            using (var context = new Dao.ErpEntities())
            {
                CS_JCCYY entity2 = context.CS_JCCYYSet.FirstOrDefault(c => c.BH == entity.BH);
                if (entity2 != null)
                {
                    entity2.MC = entity.MC;
                    return context.SaveChanges();
                }
                return 0;
            }
        }

        /// <summary>
        /// 删除一条记录
        /// </summary>
        /// <param name="id">记录Id</param>
        /// <returns>操作影响的行数</returns>
        public int Delete(string bh)
        {
            using (var context = new Dao.ErpEntities())
            {
                CS_JCCYY entity = context.CS_JCCYYSet.FirstOrDefault(c => c.BH == bh);
                if (entity != null)
                {
                    context.DeleteObject(entity);
                    return context.SaveChanges();
                }
                return 0;
            }
        }

        /// <summary>
        /// 查询一条记录
        /// </summary>
        /// <param name="id">记录Id</param>
        /// <returns>查询结果实体</returns>
        public CS_JCCYY Query(string bh)
        {
            using (var context = new Dao.ErpEntities())
            {
                CS_JCCYY entity = context.CS_JCCYYSet.FirstOrDefault(c => c.BH == bh);
                return entity;
            }
        }

        public bool Exist(string bh, string mc)
        {
            using (var context = new Dao.ErpEntities())
            {
                CS_JCCYY entity = context.CS_JCCYYSet.FirstOrDefault(c => c.BH == bh || c.MC == mc);
                return entity != null;
            }
        }

        /// <summary>
        /// 查询所有记录
        /// </summary>
        /// <returns>查询结果集</returns>
        public List<CS_JCCYY> QueryAll()
        {
            using (var context = new Dao.ErpEntities())
            {
                var q = from c in context.CS_JCCYYSet
                        select c;

                List<CS_JCCYY> list = new List<CS_JCCYY>();
                if (q.Count() > 0)
                {
                    foreach (var c in q)
                    {
                        list.Add(c);
                    }
                }
                return list;
            }
        }
    }
}
